home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
drdobbs
/
1989
/
01
/
mod2cp.asc
< prev
next >
Wrap
Text File
|
1989-01-02
|
9KB
|
446 lines
_C++ VERSUS MODULA-2_
by Scott Ladd
[LISTING ONE]
=======================
Listing 1: COMPLEX.HPP
=======================
// Header: Complex
// Version: 1.00
// Date: 10-Sep-1988
// Language: C++
// Purpose: Provides the class "complex" for C++ programs.
// Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
#include "stream.hpp"
class complex
{
private:
double real; // real part
double imag; // imaginary part
public:
// constructors
complex (void)
{
real = 0.0;
imag = 0.0;
}
complex (complex &c);
complex (double &r, double &i);
// value extraction methods
double get_real (void);
double get_imag (void);
// assignment method
void assign (double &r, double &i);
// calculation methods
complex operator = (complex &c);
complex operator + (complex &c);
complex operator - (complex &c);
complex operator * (complex &c);
complex operator / (complex &c);
// output method
friend ostream& operator << (ostream &s, complex &c);
};
[LISTING TWO]
=======================
Listing 2: COMPLEX.HPP
=======================
// Module: Complex
// Version: 1.00
// Date: 10-Sep-1988
// Language: C++
// Purpose: Provides the class "complex" for C++ programs.
// Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
#include "complex.hpp"
#include "stream.hpp"
// constructor: copy initializer
complex::complex (complex &c)
{
real = c.real;
imag = c.imag;
}
// constructor: real and imaginary parts specified
complex::complex (double &r, double &i)
{
real = r;
imag = i;
}
// retrieve real portion
double complex::get_real (void)
{
return real;
}
// retrieve imaginary portion
double complex::get_imag (void)
{
return imag;
}
// set the value of a complex object to a pair of real values
void complex::assign (double &r, double &i)
{
real = r;
imag = i;
}
// set the value of a complex number
complex complex::operator = (complex &c)
{
real = c.real;
imag = c.imag;
return *this;
}
// add two complex numbers
complex complex::operator + (complex &c)
{
complex res;
res.real = real + c.real;
res.imag = imag + c.imag;
return res;
}
// subtract two complex numbers
complex complex::operator - (complex &c)
{
complex res;
res.real = real - c.real;
res.imag = imag - c.imag;
return res;
}
// multiply two complex numbers
complex complex::operator * (complex &c)
{
complex res;
res.real = (real * c.real) - (imag * c.imag);
res.imag = (imag * c.real) + (real * c.imag);
return res;
}
// divide two complex numbers
complex complex::operator / (complex &c)
{
complex res;
double r, den;
if (fabs(c.real) >= fabs(c.imag))
{
r = c.imag / c.real;
den = c.real + r * c.imag;
res.real = (real + r * imag) / den;
res.imag = (imag - r * real) / den;
}
else
{
r = c.real / c.imag;
den = c.imag + r * c.real;
res.real = (real * r + imag) / den;
res.imag = (imag * r - real) / den;
}
return res;
}
// stream output of complex number
ostream& operator << (ostream &s, complex &c)
{
char buf[80];
sprintf(buf,"%1g%+1gi",c.real,c.imag);
return (s << buf);
}
[LISTING THREE]
=======================
Listing 3: COMPTEST.CPP
=======================
// Program: CompTest
// Version: 1.00
// Date: 10-Sep-1988
// Language: C++
// Purpose: Provides the class "complex" for C++ programs.
// Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
#include "complex.hpp"
void main()
{
complex a(1.0,2.0),
b(-1.5,-5.5),
c;
cout << "a = " << a << " b = " << b << " c = " << c << "\n";
c = a + b;
cout << c << "\n";
c = c - b;
cout << c << "\n";
c = b;
cout << c << "\n";
c = b * a;
cout << c << "\n";
c = c / a;
cout << c << "\n";
c = b / b;
cout << c << "\n";
}
[LISTING FOUR]
=======================
Listing 4: COMPLEX.DEF
=======================
DEFINITION MODULE Complex;
(*
Version: 1.00
Date: 11-Sep-1988
Language: Modula-2
Purpose: Provides the type "complex" for Modula-2.
Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
*)
TYPE
COMPLEX;
PROCEDURE Create(VAR C : COMPLEX);
PROCEDURE Destroy(VAR C : COMPLEX);
PROCEDURE GetReal(C : COMPLEX) : LONGREAL;
PROCEDURE GetImag(C : COMPLEX) : LONGREAL;
PROCEDURE Assign(VAR C : COMPLEX; R, I : LONGREAL);
PROCEDURE Equate(VAR C1 : COMPLEX; C2 : COMPLEX);
PROCEDURE Add(VAR C : COMPLEX; C1, C2 : COMPLEX);
PROCEDURE Sub(VAR C : COMPLEX; C1, C2 : COMPLEX);
PROCEDURE Mult(VAR C : COMPLEX; C1, C2 : COMPLEX);
PROCEDURE Div(VAR C : COMPLEX; C1, C2 : COMPLEX);
PROCEDURE WriteComplex(C : COMPLEX);
END Complex.
[LISTING FIVE]
=======================
Listing 5: COMPLEX.MOD
=======================
IMPLEMENTATION MODULE Complex;
(*
Version: 1.00
Date: 11-Sep-1988
Language: Modula-2
Purpose: Provides the type "complex" for Modula-2.
Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
*)
FROM Str IMPORT FixRealToStr;
FROM IO IMPORT WrStr, WrChar;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;
TYPE
COMPLEX = POINTER TO RECORD
Real : LONGREAL;
Imag : LONGREAL;
END;
PROCEDURE Create(VAR C : COMPLEX);
BEGIN
NEW(C);
C^.Real := 0.0;
C^.Imag := 0.0;
END Create;
PROCEDURE Destroy(VAR C : COMPLEX);
BEGIN
DISPOSE(C);
END Destroy;
PROCEDURE GetReal(C : COMPLEX) : LONGREAL;
BEGIN
RETURN C^.Real;
END GetReal;
PROCEDURE GetImag(C : COMPLEX) : LONGREAL;
BEGIN
RETURN C^.Imag;
END GetImag;
PROCEDURE Assign(VAR C : COMPLEX; R, I : LONGREAL);
BEGIN
C^.Real := R;
C^.Imag := I;
END Assign;
PROCEDURE Equate(VAR C1 : COMPLEX; C2 : COMPLEX);
BEGIN
C1^.Real := C2^.Real;
C1^.Imag := C2^.Imag;
END Equate;
PROCEDURE Add(VAR C : COMPLEX; C1, C2 : COMPLEX);
BEGIN
C^.Real := C1^.Real + C2^.Real;
C^.Imag := C1^.Imag + C2^.Imag;
END Add;
PROCEDURE Sub(VAR C : COMPLEX; C1, C2 : COMPLEX);
BEGIN
C^.Real := C1^.Real - C2^.Real;
C^.Imag := C1^.Imag - C2^.Imag;
END Sub;
PROCEDURE Mult(VAR C : COMPLEX; C1, C2 : COMPLEX);
BEGIN
C^.Real := (C1^.Real * C2^.Real) - (C1^.Imag * C2^.Imag);
C^.Imag := (C1^.Real * C2^.Imag) - (C1^.Imag * C2^.Real);
END Mult;
PROCEDURE Div(VAR C : COMPLEX; C1, C2 : COMPLEX);
VAR
r1, r2 : LONGREAL;
BEGIN
IF ABS(C2^.Real) >= ABS(C2^.Imag) THEN
r1 := C2^.Imag / C2^.Real;
r2 := C2^.Real + r1 * C2^.Imag;
C^.Real := (C1^.Real + r1 * C1^.Imag) / r2;
C^.Imag := (C1^.Imag - r1 * C1^.Real) / r2;
ELSE
r1 := C2^.Real / C2^.Imag;
r2 := C2^.Imag + r1 * C2^.Real;
C^.Real := (C1^.Real * r1 + C1^.Imag) / r2;
C^.Imag := (C1^.Imag * r1 - C1^.Real) / r2;
END; (* IF *)
END Div;
PROCEDURE WriteComplex(C : COMPLEX);
VAR
S : ARRAY [0..10] OF CHAR;
OK : BOOLEAN;
BEGIN
FixRealToStr(C^.Real, 3, S, OK);
WrStr(S);
FixRealToStr(C^.Imag, 3, S, OK);
IF C^.Imag >= 0.0 THEN
WrChar('+');
END; (* IF *)
WrStr(S);
WrChar('i');
END WriteComplex;
END Complex.
[LISTING SIX]
=======================
Listing 6: COMPTEST.MOD
=======================
MODULE CompTest;
(*
Version: 1.00
Date: 11-Sep-1988
Language: Modula-2
Purpose: Tests the Complex module
Copyright 1988 by Scott Robert Ladd. All Rights Reserved.
*)
FROM Complex IMPORT COMPLEX,
Create, Destroy, Assign, Equate,
Add, Sub, Mult, Div, WriteComplex;
FROM IO IMPORT WrStr, WrLn;
VAR
a, b, c : COMPLEX;
r1, i1 : LONGREAL;
r2, i2 : LONGREAL;
BEGIN
r1 := 1.0;
i1 := 2.0;
r2 := -1.5;
i2 := -5.5;
Create(a);
Create(b);
Create(c);
Assign(a,r1,i1);
Assign(b,r2,i2);
Assign(c,r1,i2);
WrStr("a = ");
WriteComplex(a);
WrStr(" b = ");
WriteComplex(b);
WrStr(" c = ");
WriteComplex(c);
WrLn;
Add(c,a,b);
WriteComplex(c);
WrLn;
Sub(c,c,b);
WriteComplex(c);
WrLn;
Mult(c,b,a);
WriteComplex(c);
WrLn;
Div(c,c,a);
WriteComplex(c);
WrLn;
Div(c,b,b);
WriteComplex(c);
WrLn;
Destroy(a);
Destroy(b);
Destroy(c);
END CompTest.